//
//  TextViewController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "TextViewController.h"

#import "GSSCommunicator.h"

@implementation TextViewController

@synthesize textView;

- (id)initWithFile:(GSSObject *)aFile inFolder:(GSSObject *)aFolder {
    if (self = [super initWithNibName:@"TextViewController" bundle:nil]) {
        self.file = aFile;
        self.folder = aFolder;
    }
    return self;
}

- (void)dealloc {
    [textView release];
    [super dealloc];
}

- (void)reloadData {
    NSString *text = [NSString stringWithContentsOfFile:self.file.dataCachePath
                                               encoding:NSUTF8StringEncoding
                                                  error:NULL];
    if (text)
        self.textView.text = text;
}


//MARK: -
//MARK: UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                              initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                              target:self
                                              action:@selector(edit)] autorelease];    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self
                      selector:@selector(keyboardWillShow:)
                          name:UIKeyboardWillShowNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(keyboardWillHide:)
                          name:UIKeyboardWillHideNotification
                        object:nil];    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


//MARK: -
//MARK: Notification Handlers

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardBounds = [[[notification userInfo]
                              objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    CGRect frame = self.textView.frame;
    frame.size.height -= keyboardBounds.size.height;
    self.textView.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    CGRect keyboardBounds = [[[notification userInfo]
                              objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    CGRect frame = self.textView.frame;
    frame.size.height += keyboardBounds.size.height;
    self.textView.frame = frame;
}


//MARK: -
//MARK: Button actions

- (void)edit {
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                               target:self
                                               action:@selector(save)] autorelease];    
    self.textView.editable = YES;
    [self.textView becomeFirstResponder];
}

- (void)save {
    self.textView.editable = NO;
    GSSCommunicator *communicator = [GSSCommunicator defaultCommunicator];
    NSData *data = [self.textView.text dataUsingEncoding:NSUTF8StringEncoding];
    [communicator createFile:self.file withData:data inFolder:self.folder];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                               target:self
                                               action:@selector(edit)] autorelease];
    [self.textView resignFirstResponder];
    if (file.versioned)
        latestVersion++;
}

@end
